import { tmsUserPage, tmsUserInsertOrUpdate, tmsUserDel, selectList, updatePassword, resetPassword } from '../services/tmsUser';
import React from 'react';
import { Input, message, Tooltip } from 'antd';

/**
 * 客户数据模型
 */

//表格列表信息
const columns = [
  {
    title: '姓名',
    dataIndex: 'cname',
    width: '120px',
    editable: true,
    ellipsis: true,
    fixed: 'left',
  },
  {
    title: '用户名',
    dataIndex: 'userName',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '性别',
    dataIndex: 'sex',
    width: '100px',
    editable: true,
    ellipsis: true,
    render: (text, record, index) => {
      if (text === '1') {
        return <span>男</span>;
      } else if (text === '0') {
        return <span>女</span>;
      } else {
        return <span />;
      }
    },
  },
  {
    title: '年龄',
    dataIndex: 'age',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '职称',
    dataIndex: 'jobTitle',
    width: '140px',
    editable: true,
    ellipsis: true,
    render: (text, record, index) => {
      switch (text) {
        case 'ADMIN':
          return <span>系统管理员</span>;
        case 'SERVICE':
          return <span>客服</span>;
        case 'SERVICE_MANAGER':
          return <span>客服主管</span>;
        case 'DISPATCH':
          return <span>调度</span>;
        case 'DISPATCH_MANAGER':
          return <span>调度主管</span>;
        case 'OPERATOR':
          return <span>运营</span>;
        case 'OPERATOR_MANAGER':
          return <span>运营主管</span>;
        case 'TEAM_LEADER':
          return <span>总助</span>;
        case 'HR':
          return <span>人事</span>
        case 'CFO':
          return <span>财务</span>;
        default:
          return <span />;
      }
    },
  },
  {
    title: '联系方式',
    dataIndex: 'phone',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '邮箱',
    dataIndex: 'email',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '身份证号',
    dataIndex: 'idCard',
    width: '200px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '祖籍',
    dataIndex: 'ancestral',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '现居住地',
    dataIndex: 'addr',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '婚姻状况',
    dataIndex: 'maritalStatus',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '学历',
    dataIndex: 'eduGroud',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '毕业时间',
    dataIndex: 'graduationDate',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '毕业院校',
    dataIndex: 'graduationSchool',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '入职时间',
    dataIndex: 'entryDate',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '离职时间',
    dataIndex: 'dimissionDate',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '我司参保时间段',
    dataIndex: 'inSocialDate',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '银行卡号',
    dataIndex: 'bankCard',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '银行卡所属银行',
    dataIndex: 'bankName',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '紧急联系人姓名',
    dataIndex: 'emergencyContact',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '紧急联系人电话',
    dataIndex: 'emergencyPhone',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '紧急联系人关系',
    dataIndex: 'emergencyRela',
    width: '120px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '排序值',
    dataIndex: 'sortVal',
    width: '100px',
    editable: true,
    ellipsis: true,
  },
  {
    title: '是否离职',
    dataIndex: 'isValid',
    width: '100px',
    editable: true,
    ellipsis: true,
    render: (text, record, index) => {
      if (text === 1) {
        return <span>否</span>;
      } else if (text === 0) {
        return <span>是</span>;
      } else {
        return <span />;
      }
    },
  },
];

// 搜索框字段
const searchFields = [
  {
    cols: [
      {
        md: 6,
        label: '用户名',
        name: 'userName',
        component: <Input placeholder="请输入用户名" />,
      },
      {
        md: 6,
        label: '中文名',
        name: 'cname',
        component: <Input placeholder="请输入中文名" />,
      },
      {
        md: 6,
        label: '身份证',
        name: 'idCard',
        component: <Input placeholder="请输入身份证" />,
      },
    ],
  },
];

// 校验规则
const requiredCols = {
  姓名: [
    {
      required: 'true',
      message: `请输入姓名!`,
    },
  ],
  用户名: [
    {
      required: 'true',
      message: `请输入用户名!`,
    },
  ],
  性别: [
    {
      required: 'true',
      message: `请输入姓别!`,
    },
  ],
  联系方式: [
    {
      required: 'true',
      message: `请输入联系方式!`,
    },
  ],
  身份证号: [
    {
      required: 'true',
      message: `请输入身份证号!`,
    },
  ],
};

export default {
  namespace: 'tmsUser',

  state: {
    searchFields: searchFields, // 搜索框信息
    searchValues: {}, // 搜索框里的值
    pageList: [], // 表格数据
    pagination: {
      pageSize: 10,
      current: 1,
      size: 'small',
      showSizeChanger: true,
      showQuickJumper: true,
    }, // 表格分布数据
    columns: columns, // 表格列信息
    editId: '', // 编辑行id
    selectionIds: [], // 表格选择ID
    requiredCols: requiredCols, // 必填字段
  },

  effects: {
    // 获取分页数据
    *page({ payload }, { call, put }) {
      const res = yield call(tmsUserPage, payload);
      yield put({
        type: 'page_r',
        payload: res,
      });
    },
    *save({ payload }, { call, put }) {
      const res = yield call(tmsUserInsertOrUpdate, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *del({ payload }, { call, put }) {
      const res = yield call(tmsUserDel, payload);
      yield put({
        type: 'del_r',
        payload: res,
      });
    },
    *selectList({ payload }, { call, put }) {
      const res = yield call(selectList, payload);
      yield put({
        type: 'selectList_r',
        payload: res,
      });
    },
    *updatePassword({ payload }, { call, put }) {
      const res = yield call(updatePassword, payload);
      yield put({
        type: 'save_r',
        payload: res,
      });
    },
    *resetPassword({ payload }, { call, put }) {
      const res = yield call(resetPassword, payload);
      yield put({
        type: 'save_r',
        payload: res,
      })
    }
  },

  reducers: {
    // 新增
    handleAdd(state) {
      const { pageList, pagination } = state;
      let addList = pageList.filter(page => {
        return page.id === 0;
      });
      if (addList.length) {
        message.warning('请提交后再添加！');
        return {
          ...state,
        };
      } else {
        pageList.unshift({ id: 0, customerName: '新增' });
        const newPagination = {
          pages: pagination.pages,
          pageSize: pagination.pageSize,
          total: pagination.total + 1,
          current: pagination.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pageList: pageList,
          pagination: newPagination,
        };
      }
    },
    // 绑定搜索框数据
    handleSearchChange(state, action) {
      const { searchValues } = state;
      const newSearchForm = {
        ...searchValues,
        ...action.payload,
      };
      return {
        ...state,
        searchValues: newSearchForm,
      };
    },
    // 处理编辑
    handleEdit(state, action) {
      return {
        ...state,
        editId: action.payload,
      };
    },

    // 改变分页插件
    handlePagination(state, action) {
      const { payload } = action;
      const { pagination } = state;
      let newPagination = {
        ...pagination,
        ...payload,
      };
      return {
        ...state,
        pagination: newPagination,
      };
    },

    // 表格选中Id
    handleSelection(state, action) {
      const { payload } = action;
      return {
        ...state,
        selectionIds: payload,
      };
    },
    // 分页数据请求回来后处理
    page_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        const list = payload.result.records;
        const pagination = {
          pages: payload.result.pages,
          pageSize: payload.result.size,
          total: payload.result.total,
          current: payload.result.current,
          size: 'small',
          showSizeChanger: true,
          showQuickJumper: true,
        };
        return {
          ...state,
          pagination: pagination,
          pageList: list,
          editId: '',
        };
      } else {
        return {
          ...state,
        };
      }
    },
    // 处理保存
    save_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('保存成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` : '保存失败');
      }
      return {
        ...state,
      };
    },
    // 处理删除
    del_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        message.success('删除成功！');
      } else {
        message.error(`${payload.errorMsg}` ? `${payload.errorMsg}` :'删除失败！');
      }
      return {
        ...state,
      };
    },
    // 处理搜索下垃
    selectList_r(state, action) {
      const { payload } = action;
      if (payload && payload.code === 0) {
        return {
          ...state,
          selectList: payload.result,
        };
      } else {
        message.error('获取客户下拉框数据失败！');
      }
    },
  },
};
